#ifndef __SQLITE_LOCK_COUNTER_H
#define __SQLITE_LOCK_COUNTER_H

#include <apr_portable.h>
#include <stdint.h>
#include "../sqlite/sqlite3.h"

/** Counter class for the lock on this database. */
class SQLiteLockCounter
{
   public:
      /** Initialises the counter to zero and enters the SQLite mutex
       * lock. */
      SQLiteLockCounter(sqlite3_mutex *idbmutex);
      /** Gives an error if the mutex counter is non-zero and leaves
       * the SQLite mutex lock. */
      ~SQLiteLockCounter();
      uint64_t incrementReferenceCount();
      uint64_t decrementReferenceCount();
   private:
      /** This class is not copyable. */
      SQLiteLockCounter(const SQLiteLockCounter &o);
      /** This class is not copyable. */
      SQLiteLockCounter &operator=(const SQLiteLockCounter &o);
      /** Reference count for the mutex, to be freed when it reaches
       * zero. */
      uint64_t counter;
      /** APR OS-specific thread ID. */
      apr_os_thread_t holdingthread;
      /** SQLite database mutex. */
      sqlite3_mutex *dbmutex;
};

#endif
